package process2

import (
	"bufio"
	"chatOnline/common/message"
	"encoding/json"
	"fmt"
	"os"
)

type ActionProcess struct {
}

func (this *ActionProcess) DealFileLoad(mes *message.Message) (err error) {

	mes2 := message.FileLoadMes{}
	err = json.Unmarshal([]byte(mes.Data), &mes2)
	if err != nil {
		fmt.Println("json unmarshal err=", err.Error())
		return
	}

	file, err := os.OpenFile(mes2.FileName, os.O_WRONLY|os.O_CREATE, 0666)
	if err != nil {
		fmt.Println("server open file err=", err.Error())
		return
	}
	defer file.Close()
	writer := bufio.NewWriter(file)
	n := mes2.FileLen

	if n == 0 {
		fmt.Println("file load success")
		return
	}
	writer.Write(mes2.FileData[:n])
	writer.Flush()

	return
}
